<style scoped>
  .register{
    height:100%;
    background: #edf1f5;
  }
  input{
    border:none;
  }
  .header{
    display: flex;
    flex-direction: column;
  }
  .head-top{
    width:100%;
    height:10px;
    background: transparent;
  }
  .head{
    height:60px;
    display: flex;
    justify-content: center;
    align-items:center;
    border-bottom: 1px solid #E9E9E9;
    box-sizing: border-box;
    background: #ffffff;
    padding-top: 10px;
  }
  .head-left{
    position: absolute;
    width:40px;
    height:60px;
    left:0;
    top:0;
    color: #7f7a8e;
    text-align: center;
  }
  .head-left h1{
    margin-top: 22px;
  }
  .head-middle{
    font-size: 16px;
  }
  .head-right{
    position: absolute;
    right:10px;
    top:15px;
  }
  .content{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    margin-top: 80px;
    /*height:100%;*/
  }
  .content>h3{
    margin-bottom: 20px;
  }
  .input{
    width:100%;
  }
  .input>div{
    margin:0 5%;
  }
  .input>div>.zhmm{
    background: #ffffff;
    border-radius: 10px;
  }
  .input>div>.zhmm>div{
    display: flex;
    align-items: center;
  }
  .input>div>.zhmm>div>label{
    flex: 2;
    display: flex;
    justify-content: center;
    align-items:center;
  }
  .input>div>.zhmm>div input{
    flex: 7;
    display: flex;
    align-items: center;
    /*height:80%;*/
    padding-left: 10px;
  }
  .input>div>.zhmm>div{
    border-bottom: 1px solid #e9e9e9;
    box-sizing: border-box;
  }
  .submit{
    margin-top: 30px;
    border-radius: 10px;
    background: #4D66FF;
  }
  .submit a{
    height:100%;
    width:100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .submit h3{
    color: #ffffff;
  }
  @media screen and (max-width: 320px) {
    .input>div>.zhmm>div{
      height:43px;
    }
    .submit{
      height:43px;
    }
    .input>div>.zhmm input{
      font-size: 1.2rem;
    }
  }
  @media screen and (max-width:375px) and (min-width:321px) {
    .input>div>.zhmm>div{
      height:50px;
    }
    .submit{
      height:50px;
    }
    .input>div>.zhmm input{
      font-size: 1.4rem;
    }
  }
  @media screen and (min-width: 376px){
    .input>div>.zhmm>div{
      height:60px;
    }
    .submit{
      height:60px;
    }
    .input>div>.zhmm input{
      font-size: 1.6rem;
    }
  }
  .confirm{
    display: flex;
    justify-content: center;
    margin-top: 8px;
  }
  /*验证*/
  .test{
    margin-left: 20px;
    margin-top: 4px;
    color: #e43937;
  }

  .password{
    position: relative;
  }
  .password>p{
    position: absolute;
    top:14px;
    right:10px;
  }
</style>

<template>
  <div class="register" ref="nana">
    <div class="header">
      <div class="head">
        <div class="head-left" @click="goBack"><h1 class="iconfont icon-xiangzuo1"></h1></div>
        <div class="head-middle"><h3>重置密码</h3></div>
      </div>
    </div>


    <div class="content">
      <h3>重置登录密码</h3>
      <div class="input">
        <div>
          <div class="zhmm">
            <div class="password">
              <input type="password" placeholder="请输入新密码" v-model="pwd1" id="pwd1">
              <p class="iconfont" v-bind:class="{'icon-attention_forbid': isVisible1,'icon-attention_forbid1': isVisible2}" @click="pwdIsVisible1"></p>
            </div>
            <small class="test" v-if="hasPassword">{{testPassword}}</small>
            <div class="password">
              <input type="password" placeholder="请再次输入新密码" v-model="pwd2" id="pwd2">
              <p class="iconfont" v-bind:class="{'icon-attention_forbid': isVisible3,'icon-attention_forbid1': isVisible4}" @click="pwdIsVisible2"></p>
            </div>
            <small class="test" v-if="hasPwd">{{testPwd}}</small>
          </div>
          <div class="submit" @click="next">
            <router-link to=""><h3>确认修改</h3></router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import axios from 'axios'
  import { Toast } from 'mint-ui'
  import host from '../config.js'

  export default {
    data () {
      return {
        pwd1: null,
        pwd2: null,
        hasPassword: false,
        hasPwd: false,
        testPassword: null,
        testPwd: "两次密码不一致",
        getReset: null,
        //        密码是否可见
        isVisible1: true,
        isVisible2: false,
        isVisible3: true,
        isVisible4: false,
      }
    },
    watch: {
      pwd1: function (newValue, oldValue) {
        if (newValue !== '' || newValue !== null){
          this.hasPassword = false
        }
      },
      pwd2: function (newValue, oldValue) {
        if (newValue !== '' || newValue !== null){
          this.hasPwd = false
        }
      }
    },
    mounted () {
      let h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
      let h1 = h
      this.$refs.nana.style.height = h1 + 'px'
      this.getReset = this.$store.state.sendCode
    },
    methods: {
      goBack () {
        this.$router.go(-1)
      },
      send () {

      },
      next () {
        let that = this
        if(this.pwd1 === null && this.pwd2 === null){
          this.hasPassword = true
          this.testPassword = '密码不能为空'
          return
        }else{
          if(this.pwd1.trim().length > 20 || this.pwd2.trim().length < 6 || this.pwd1.trim().length > 20 || this.pwd2.trim().length < 6){
            this.hasPassword = true
            this.testPassword = '密码必须为6-20位'
            return
          }
        }
        if(this.pwd1 !== this.pwd2) {
          this.hasPwd = true
          return
        }
        let sendData = {
          "phone": that.getReset.phone,
          "passwd": that.pwd1,
          "code": that.getReset.message
        }
        axios({
          method: 'post',
          url: host.host + "/api/usr/forget",
          data: JSON.stringify(sendData)
        }).then(function (data) {
          console.log(data)
          if(data.data.code != 200){
            return
          }else{
            Toast({
              message: data.data.msg,
              duration: 1000
            })
            let interval = window.setTimeout(function() {
              that.$router.push({'path':'/login'})
            }, 1000)
          }
        })
      },
      pwdIsVisible1 () {
        this.isVisible1 = !this.isVisible1
        this.isVisible2 = !this.isVisible2
        if(document.getElementById('pwd1').getAttribute('type') === 'password'){
          document.getElementById('pwd1').setAttribute('type','text')
        }else{
          document.getElementById('pwd1').setAttribute('type','password')
        }
      },
      pwdIsVisible2 () {
        this.isVisible3 = !this.isVisible3
        this.isVisible4 = !this.isVisible4
        if(document.getElementById('pwd2').getAttribute('type') === 'password'){
          document.getElementById('pwd2').setAttribute('type','text')
        }else{
          document.getElementById('pwd2').setAttribute('type','password')
        }
      }
    }
  }
</script>

